import { defineEventHandler, getQuery } from 'h3';

// 模拟定价模型数据
const mockPricingModels = [
  {
    id: '1',
    name: '酒店管理系统定价',
    type: 'subscription',
    description: '酒店管理系统的订阅定价模型',
    resourceId: '1',
    resourceName: '酒店管理系统',
    customerPricing: {
      currency: 'CNY',
      basePrice: 999,
      unit: 'month',
      features: ['基础功能', '技术支持', '培训'],
      tiers: [
        {
          name: '基础版',
          price: 599,
          features: ['基础功能'],
          limits: {
            maxRooms: 50,
            maxUsers: 10,
          },
        },
        {
          name: '标准版',
          price: 999,
          features: ['基础功能', '技术支持'],
          limits: {
            maxRooms: 200,
            maxUsers: 25,
          },
        },
        {
          name: '高级版',
          price: 1999,
          features: ['完整功能', '技术支持', '培训', '定制开发'],
          limits: {
            maxRooms: 1000,
            maxUsers: 50,
          },
        },
      ],
    },
    platformPricing: {
      currency: 'CNY',
      cost: 299,
      margin: 700,
      revenueShare: 0.3,
      commission: 0.1,
    },
    status: 'active',
    createdAt: '2024-01-01T00:00:00Z',
    updatedAt: '2024-01-15T10:30:00Z',
    createdBy: 'admin',
  },
  {
    id: '2',
    name: '宠物店管理系统定价',
    type: 'subscription',
    description: '宠物店管理系统的订阅定价模型',
    resourceId: '2',
    resourceName: '宠物店管理系统',
    customerPricing: {
      currency: 'CNY',
      basePrice: 599,
      unit: 'month',
      features: ['基础功能'],
      tiers: [
        {
          name: '基础版',
          price: 399,
          features: ['基础功能'],
          limits: {
            maxPets: 100,
            maxUsers: 5,
          },
        },
        {
          name: '标准版',
          price: 599,
          features: ['基础功能', '技术支持'],
          limits: {
            maxPets: 300,
            maxUsers: 10,
          },
        },
        {
          name: '高级版',
          price: 999,
          features: ['完整功能', '技术支持', '培训'],
          limits: {
            maxPets: 500,
            maxUsers: 20,
          },
        },
      ],
    },
    platformPricing: {
      currency: 'CNY',
      cost: 199,
      margin: 400,
      revenueShare: 0.25,
      commission: 0.08,
    },
    status: 'draft',
    createdAt: '2024-01-12T14:30:00Z',
    updatedAt: '2024-01-15T16:20:00Z',
    createdBy: 'admin',
  },
  {
    id: '3',
    name: '教育管理系统定价',
    type: 'subscription',
    description: '教育管理系统的订阅定价模型',
    resourceId: '3',
    resourceName: '教育管理系统',
    customerPricing: {
      currency: 'CNY',
      basePrice: 1999,
      unit: 'month',
      features: ['完整功能', '技术支持', '培训'],
      tiers: [
        {
          name: '基础版',
          price: 1299,
          features: ['基础功能'],
          limits: {
            maxStudents: 1000,
            maxTeachers: 20,
          },
        },
        {
          name: '标准版',
          price: 1999,
          features: ['完整功能', '技术支持'],
          limits: {
            maxStudents: 5000,
            maxTeachers: 100,
          },
        },
        {
          name: '高级版',
          price: 3999,
          features: ['完整功能', '技术支持', '培训', '定制开发'],
          limits: {
            maxStudents: 10000,
            maxTeachers: 200,
          },
        },
      ],
    },
    platformPricing: {
      currency: 'CNY',
      cost: 599,
      margin: 1400,
      revenueShare: 0.4,
      commission: 0.15,
    },
    status: 'active',
    createdAt: '2023-12-01T00:00:00Z',
    updatedAt: '2024-01-10T11:15:00Z',
    createdBy: 'admin',
  },
  {
    id: '4',
    name: '零售POS系统定价',
    type: 'subscription',
    description: '零售POS系统的订阅定价模型',
    resourceId: '4',
    resourceName: '零售POS系统',
    customerPricing: {
      currency: 'CNY',
      basePrice: 799,
      unit: 'month',
      features: ['基础功能', '技术支持'],
      tiers: [
        {
          name: '基础版',
          price: 499,
          features: ['基础功能'],
          limits: {
            maxProducts: 500,
            maxStores: 3,
          },
        },
        {
          name: '标准版',
          price: 799,
          features: ['基础功能', '技术支持'],
          limits: {
            maxProducts: 1000,
            maxStores: 10,
          },
        },
        {
          name: '高级版',
          price: 1499,
          features: ['完整功能', '技术支持', '培训'],
          limits: {
            maxProducts: 5000,
            maxStores: 50,
          },
        },
      ],
    },
    platformPricing: {
      currency: 'CNY',
      cost: 299,
      margin: 500,
      revenueShare: 0.2,
      commission: 0.05,
    },
    status: 'active',
    createdAt: '2023-11-01T00:00:00Z',
    updatedAt: '2024-01-08T14:45:00Z',
    createdBy: 'admin',
  },
];

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const { page = 1, pageSize = 10, type, status, resourceId } = query;

  // 过滤数据
  let filteredData = [...mockPricingModels];

  if (type) {
    filteredData = filteredData.filter((model) => model.type === type);
  }

  if (status) {
    filteredData = filteredData.filter((model) => model.status === status);
  }

  if (resourceId) {
    filteredData = filteredData.filter(
      (model) => model.resourceId === resourceId,
    );
  }

  // 分页
  const pageNum = parseInt(page.toString());
  const size = parseInt(pageSize.toString());
  const start = (pageNum - 1) * size;
  const end = start + size;
  const paginatedData = filteredData.slice(start, end);

  return {
    success: true,
    data: {
      list: paginatedData,
      pagination: {
        current: pageNum,
        pageSize: size,
        total: filteredData.length,
        totalPages: Math.ceil(filteredData.length / size),
      },
    },
    message: '查询成功',
  };
});
